﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace GenericLibrary.Modeling {

    public delegate double FirstOrderOrdinaryDifferentialEquation( double x, double y );

    public interface IFirstOrderOrdinaryDifferentialEquationSolver {

        double DeltaY( FirstOrderOrdinaryDifferentialEquation equation, double x, double y, double deltaX );
    }

    public class RK45Solver : IFirstOrderOrdinaryDifferentialEquationSolver {

        #region IFirstOrderOrdinaryDifferentialEquationSolver Members

        public double DeltaY( FirstOrderOrdinaryDifferentialEquation equation, double x, double y, double deltaX ) {
            try {
                double k1 = equation( x, y );
                double k2 = equation( x + deltaX / 2, y + k1 * deltaX / 2 );
                double k3 = equation( x + deltaX / 2, y + k2 * deltaX / 2 );
                double k4 = equation( x + deltaX, y + k3 * deltaX );
                return k4 * deltaX;
            } catch( Exception ) {
                Debug.Assert( false );
                throw;
            }
        }

        #endregion
    }

    
}
